﻿Imports System.Data.OleDb
Imports System.Globalization
Imports System.Globalization.CultureInfo
Imports System.IO
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            savec.Visible = False
        End If
        DisplayData()
    End Sub
    
    Protected Sub DisplayData()
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim sql = "SELECT * FROM userprofiles WHERE TravellerName=@f1"
        Dim cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        conn.Open()
        Dim profileDr = cmd.ExecuteReader()
        profileDr.Read()
        Dim newEmailAddress = ""
        Dim newDescription = ""
        Dim newDOB = ""
        Dim newLocation = ""
        Dim newProfession = ""
        Dim newSmoker = ""
        Dim newDrinker = ""
        Dim newEducationLevel = ""
        Dim newMaritalStatus = ""
        If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
        If Not IsDBNull(profileDr("EmailAddress")) Then newEmailAddress = profileDr.Item("EmailAddress")
        If Not IsDBNull(profileDr("DOB")) Then newDOB = profileDr.Item("DOB")
        If Not IsDBNull(profileDr("Location")) Then newLocation = profileDr.Item("Location")
        If Not IsDBNull(profileDr("Description")) Then newDescription = profileDr.Item("Description")
        If Not IsDBNull(profileDr("Profession")) Then newProfession = profileDr.Item("Profession")
        If Not IsDBNull(profileDr("Smoker")) Then newSmoker = profileDr.Item("Smoker")
        If Not IsDBNull(profileDr("Drinker")) Then newDrinker = profileDr.Item("Drinker")
        If Not IsDBNull(profileDr("EducationLevel")) Then newEducationLevel = profileDr.Item("EducationLevel")
        If Not IsDBNull(profileDr("MaritalStatus")) Then newMaritalStatus = profileDr.Item("MaritalStatus")
        If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
        description.Text = newDescription
        email.Text = newEmailAddress
        smoker.SelectedValue = newSmoker
        drinker.SelectedValue = newDrinker
        dd_userlocation.SelectedValue = newLocation
        dob.Text = newDOB
        educationlevel.SelectedValue = newEducationLevel
        profession.SelectedValue = newProfession
        maritalstatus.SelectedValue = newMaritalStatus

        conn.Close()

    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        
        Dim sql
        Dim cmd
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        If Not fu_avatar.HasFile Then
            sql = "UPDATE userprofiles SET EmailAddress=@f1, DOB=@f2, Location=@f3, Description=@f4, Profession=@f5 , Smoker=@f6, Drinker=@f7, EducationLevel=@f8, MaritalStatus=@f9 WHERE TravellerName=@f10"
            cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", email.Text)
            cmd.Parameters.AddWithValue("@f2", DateTime.ParseExact(dob.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))
            cmd.Parameters.AddWithValue("@f3", dd_userlocation.SelectedValue)
            cmd.Parameters.AddWithValue("@f4", description.Text)
            cmd.Parameters.AddWithValue("@f5", profession.SelectedValue)
            cmd.Parameters.AddWithValue("@f6", smoker.SelectedValue)
            cmd.Parameters.AddWithValue("@f7", drinker.SelectedValue)
            cmd.Parameters.AddWithValue("@f8", educationlevel.SelectedValue)
            cmd.Parameters.AddWithValue("@f9", maritalstatus.SelectedValue)
            cmd.Parameters.AddWithValue("@f10", User.Identity.Name)
        Else
            sql = "UPDATE userprofiles SET EmailAddress=@f1, DOB=@f2, Location=@f3, Description=@f4, Profession=@f5 , Smoker=@f6, Drinker=@f7, EducationLevel=@f8, MaritalStatus=@f9, AvatarURL=@f10 WHERE TravellerName=@f11"
            cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", email.Text)
            cmd.Parameters.AddWithValue("@f2", DateTime.ParseExact(dob.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))
            cmd.Parameters.AddWithValue("@f3", dd_userlocation.SelectedValue)
            cmd.Parameters.AddWithValue("@f4", description.Text)
            cmd.Parameters.AddWithValue("@f5", profession.SelectedValue)
            cmd.Parameters.AddWithValue("@f6", smoker.SelectedValue)
            cmd.Parameters.AddWithValue("@f7", drinker.SelectedValue)
            cmd.Parameters.AddWithValue("@f8", educationlevel.SelectedValue)
            cmd.Parameters.AddWithValue("@f9", maritalstatus.SelectedValue)
            cmd.Parameters.AddWithValue("@f10", "UserProfileAvatar/" & User.Identity.Name & ".jpg")
            cmd.Parameters.AddWithValue("@f11", User.Identity.Name)
            Dim fileLocationOnServerHardDisk = Request.MapPath("UserProfileAvatar") & "/" & User.Identity.Name & ".jpg"
            fu_avatar.SaveAs(fileLocationOnServerHardDisk)
        End If
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        
        DisplayData()
        savec.Visible = True
    End Sub

End Class